package neusoft.yuejian_11.model;

import java.util.List;

import neusoft.yuejian_11.bean.IsMatchn;
import neusoft.yuejian_11.bean.MatchBean;
import neusoft.yuejian_11.bean.MatchnBean;
import neusoft.yuejian_11.bean.MatchnInBean;
import neusoft.yuejian_11.interF.Const;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.interF.RetrofitService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by neusoft on 2017/10/9.
 */

public class MatchModel {
    private Retrofit retrofit;
    public MatchModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //比赛列表
    public void matchList(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<MatchBean>> call = retrofitService.getAllMatches();
        Callback<List<MatchBean>> callback = new Callback<List<MatchBean>>() {
            @Override
            public void onResponse(Call<List<MatchBean>> call, Response<List<MatchBean>> response) {
                List<MatchBean> matchBeanList = response.body();
                listener.onSuccess(matchBeanList,1);
            }

            @Override
            public void onFailure(Call<List<MatchBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //比赛详情
    public void matchDetail(String matchId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<MatchBean> call = retrofitService.getMatchById(matchId);
        Callback<MatchBean> callback = new Callback<MatchBean>() {
            @Override
            public void onResponse(Call<MatchBean> call, Response<MatchBean> response) {
                MatchBean matchBeanDetail = response.body();
                listener.onSuccess(matchBeanDetail,1);
            }

            @Override
            public void onFailure(Call<MatchBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //比赛报名
    public void matchnn(int userId, int matchId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<MatchnBean> call = retrofitService.getMatchnById(userId,matchId);
        Callback<MatchnBean> callback = new Callback<MatchnBean>() {
            @Override
            public void onResponse(Call<MatchnBean> call, Response<MatchnBean> response) {
                MatchnBean matchnBean = response.body();
                listener.onSuccess(matchnBean,4);
            }

            @Override
            public void onFailure(Call<MatchnBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //判断是否报名
    public void IsAddMatchn(int userId, String matchId,final RetrofitListener listener) {
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call call=retrofitService.IsAddMatchn(userId,String.valueOf(matchId));
        call.enqueue(new Callback<IsMatchn>() {
            @Override
            public void onResponse(Call<IsMatchn> call, Response<IsMatchn> response) {
                IsMatchn isMatchn=response.body();
                listener.onSuccess(isMatchn,5);
            }
            @Override
            public void onFailure(Call<IsMatchn> call, Throwable t) {

            }
        });
    }
    //活动报名列表
    public void matchnIn(int userId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<MatchnInBean>> call = retrofitService.getMatchReg(userId);
        Callback<List<MatchnInBean>> callback = new Callback<List<MatchnInBean>>() {
            @Override
            public void onResponse(Call<List<MatchnInBean>> call, Response<List<MatchnInBean>> response) {
                List<MatchnInBean> matchnInBeanList = response.body();
                listener.onSuccess(matchnInBeanList,1);
            }

            @Override
            public void onFailure(Call<List<MatchnInBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
}
